package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.PageData;
import com.woniu.entity.dto.LikeDTO;
import com.woniu.entity.vo.LikeVO;
import com.woniu.mapper.LikeMapper;
import com.woniu.pojo.*;

import com.woniu.service.ICommentService;
import com.woniu.service.ILikeService;
import com.woniu.service.IMessageService;
import com.woniu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaosong
 * @since 2023-04-01
 */
@Service
public class LikeServiceImpl extends ServiceImpl<LikeMapper, Like> implements com.woniu.service.ILikeService {

    @Autowired
    private  LikeMapper likeMapper;
    @Autowired
    private ILikeService likeService;
    @Autowired
    private IMessageService messageService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ICommentService commentService;

//    分页查询
    @Override
    public PageData search(Integer currentPage, Integer pageSize, LikeDTO likeDTO) {

//        开启分页
        IPage<LikeVO> page = new Page<>(currentPage,pageSize);

        IPage<LikeVO> likeVOS = likeMapper.selectall(page,likeDTO);


//         封装PageData对象
        PageData pageData = new PageData(likeVOS.getTotal(),likeVOS.getRecords());

        return pageData;
    }

    @Override
    public List<Like> selectByid(Integer id) {
        QueryWrapper<Like> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        List<Like> likes = likeMapper.selectList(wrapper);
        return likes;
    }

    //按照id查询
    @Override
    public List<Like> selectById(Integer id) {

        QueryWrapper<Like> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
         List<Like> likes = likeMapper.selectList(wrapper);
        return likes;
    }



    @Override
    public boolean add(Like like) {
        QueryWrapper<Like> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", like.getUserId());
        wrapper.eq("comment_id", like.getCommentId());
        if (likeService.list(wrapper).size() == 0) {
            likeService.save(like);
            Message message = new Message();
            Comment comment = commentService.getById(like.getCommentId());
            User receiveUser = userService.getById(comment.getUserId());
            User sendUser = userService.getById(like.getUserId());
            message.setUserId(receiveUser.getId());
            message.setUserName(receiveUser.getName());
            message.setTitle("您收到了来自'" + sendUser.getName() + "'用户的点赞");
            message.setContent("您的'" + comment.getMessage() + "'评论收到了来自'" + sendUser.getName() + "'用户的点赞");
            message.setType((byte) 0);
            message.setStatus((byte) 0);
            messageService.save(message);

        } else {
            likeService.remove(wrapper);
        }
        return true;
    }

    @Override
    public Integer getUnread(Integer id) {
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        wrapper.eq("type",0);
        wrapper.eq("status",0);
        Integer unreadLike = messageService.list(wrapper).size();
        return unreadLike;
    }

    @Override
    public Integer updateLike(Like like) {
        return likeMapper.updateById(like);
    }

    @Override
    public boolean saveLike(Like like) {
        //        避免重复点赞，需要判断新增的点赞是否存在
        QueryWrapper<Like> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",like.getUserId());
        wrapper.eq("comment_id",like.getCommentId());
        Like like1 = likeMapper.selectOne(wrapper);
        if (like1 != null){
            return false;
        }else {
            likeService.add(like);
            return true;
        }
    }
}
